﻿using System.Web.Mvc;
using MRGSP.SurveyMaster.Core.Security;
using MRGSP.SurveyMaster.Core.Service;

namespace MRGSP.SurveyMaster.WebUI.Controllers
{

    [HandleError]
    public class AccountController : Controller
    {
        private readonly IFormsAuthentication formsAuth;
        private readonly IUserService userService;

        public AccountController(IFormsAuthentication formsAuth, IUserService userService)
        {
            this.formsAuth = formsAuth;
            this.userService = userService;
        }

        public ActionResult LogOn()
        {
            return View();
        }
        [HttpPost]
        public ActionResult LogOn(string login, string password, bool? rememberMe)
        {
            if (!userService.Exists(login, password))
            {
                ModelState.AddModelError("_FORM", "Login or Password not correct, please try againg");
                return View();
            }

            var role = userService.GetRole(login);
            formsAuth.SignIn(login, rememberMe ?? false, role);
            return RedirectToAction("Index", "SurveySession");
        }
       

        public ActionResult LogOff()
        {
            formsAuth.SignOut();

            return RedirectToAction("Index", "Home");
        }

    }
}
